package dk.twu.analyzer.model;

import dk.twu.analyzer.loader.SymbolLoader;
import dk.twu.analyzer.util.AnalyzerConfig;

import java.util.*;

public class SymbolList implements ISymbol {
    private final Map<String, Symbol> symbolMap;

    public SymbolList(Map<String, Symbol> symbolMap) {
        this.symbolMap = symbolMap;
    }

    public static SymbolList setupSymbolList(AnalyzerConfig analyzerConfig, Collection<Exchange> exchanges) {
        Map<String, Symbol> symbolMap = new HashMap<>();
        exchanges.stream().forEach(ex -> {
            symbolMap.putAll(SymbolLoader.loadSymbolMap(analyzerConfig.getBaseDir(), analyzerConfig.getSymbolListFile
                    (), ex));
        });
        return new SymbolList(symbolMap);
    }

    public Collection<Symbol> findSymbolNameContains(String text) {
        Collection<Symbol> result = new ArrayList<>();
        symbolMap.entrySet().stream().filter(entry -> entry.getValue().getName().toUpperCase().contains(text
                .toUpperCase())).forEach(entry -> result.add(entry.getValue()));
        return result;
    }

    @Override
    public Optional<Symbol> getSymbol(String symbolCode) {
        Symbol s = symbolMap.get(symbolCode);
        return s != null ? Optional.of(s) : Optional.empty();
    }
}
